package Iot;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

public class Base64Util {
    public static void main(String[] args) {
        //base64_1();
        //base64_2();
        base64_3();
    }

    //Header 名称是 "Authorization", 值是 base64转换过的 "ProductKey:ProductSecret"（中间有个冒号）
    //智能出纸机  8a79f1054bbdba5c4f3051a6
    private static void base64_1() {
        final Base64.Decoder decoder = Base64.getDecoder();
        final Base64.Encoder encoder = Base64.getEncoder();
        final String text = "字串文字";
        byte[] textByte;
        try {
            textByte = text.getBytes("UTF-8");
            //编码
            final String encodedText = encoder.encodeToString(textByte);
            System.out.println(encodedText);
            //解码
            System.out.println(new String(decoder.decode(encodedText), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    /**
     *@Author: caoxiabo on 2019/12/17 15:46
     *@Description: 智能出纸机 ProductKey：1b8ddfbbac8428f5965436cd   ProductSecret：8a79f1054bbdba5c4f3051a6
     */
    private static void base64_2() {
        final Base64.Decoder decoder = Base64.getDecoder();
        final Base64.Encoder encoder = Base64.getEncoder();
        final String text = "1b8ddfbbac8428f5965436cd:8a79f1054bbdba5c4f3051a6";
        byte[] textByte;
        try {
            textByte = text.getBytes("UTF-8");
            //编码
            final String encodedText = encoder.encodeToString(textByte);
            System.out.println(encodedText);
            //解码
            System.out.println(new String(decoder.decode(encodedText), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    /**
     *@Author: caoxiabo on 2019/12/17 15:49
     *@Description: appID:51e5f003eec1c     appSecret:MjkwNjMzMDAxODY2NDExODQ4Mjc0MjM4NjQwMDc0MjYwNDI
     *emqx token获取 Basic NTFlNWYwMDNlZWMxYzpNamt3TmpNek1EQXhPRFkyTkRFeE9EUTRNamMwTWpNNE5qUXdNRGMwTWpZd05ESQ==
     */
    private static void base64_3() {
        final Base64.Decoder decoder = Base64.getDecoder();
        final Base64.Encoder encoder = Base64.getEncoder();
        final String text = "51e5f003eec1c:MjkwNjMzMDAxODY2NDExODQ4Mjc0MjM4NjQwMDc0MjYwNDI";
        byte[] textByte;
        try {
            textByte = text.getBytes("UTF-8");
            //编码
            final String encodedText = encoder.encodeToString(textByte);
            System.out.println(encodedText);
            //解码
            System.out.println(new String(decoder.decode(encodedText), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
